(function () {
    'use strict';

    window.addEventListener('load', () => {

        let novelTitle = document.querySelector('.txt').textContent
        novelTitle = novelTitle.replace(/\s+/g, '')
        console.log(novelTitle)
        let authorName = document.querySelector('.sub-title').querySelector('span.txt:first-child').querySelector('a').textContent
        authorName = authorName.replace(/\s+/g, '')
        console.log(authorName)
        let novelInfo = document.querySelector('.intro').textContent
        novelInfo = novelInfo.replace(/\s+/g, '\n')
        console.log(novelInfo)
        let coverImg = document.querySelector('.wrap-pic').querySelector('img').src
        console.log(coverImg)
        let read = document.querySelector('.btns-wrap.clearfix')
        console.log(read)

        const apiUrl = 'http://127.0.0.1:27101/novel/joinNovel'

        const data = {
            "novelTitle": novelTitle,
            "authorName": authorName,
            "typeId": 16,
            "novelInfo": novelInfo,
            "coverImg": coverImg,
            "status": "draft",
        }
        window.fetch(apiUrl, {
            method: "POST", body: JSON.stringify(data), headers: {
                "Content-Type": "application/json",
            },
        }).then((resp) => {
            console.log(resp)
        });
        console.log(fetch)
    })


})();


(function () {
    'use strict';
    window.addEventListener('load', () => {


        let novelId = 0;


        let chapterTitle = document.querySelector('.chapter-title').textContent
        console.log(chapterTitle)
        let content = document.querySelector('.article').innerHTML
        content = content.replace(/<p[^>]*>(.*?)<\/p>/g, (match, pContent) => {
            return `${pContent}\n`;
        });
        console.log(content)
        let chapterOrder = document.querySelector('.chapter-title').textContent.match(/第(\d+)章/)
        chapterOrder = parseInt(chapterOrder[1])
        console.log(chapterOrder)


        const data = {
            "novelId": 0, "chapterTitle": chapterTitle, "content": content, "chapterOrder": chapterOrder,

        }

        const url = 'http://127.0.0.1:27101/novel/joinChapter'

        let a = document.querySelector('.reader-footer').lastElementChild

        console.log(a)


        const urls = 'http://127.0.0.1:27101/novel/getLastId'
        fetch(urls).then((response) => {
            // 检查响应状态是否正常
            if (response.ok) {
                return response.json();
            } else {
                throw new Error('请求失败');
            }
        }).then((datas) => {
            // 在这里处理解析后的响应数据，data就是请求成功后返回的数据
            data.novelId = datas
            console.log(data.novelId);
            fetch(url, {
                method: 'post', body: JSON.stringify(data), headers: {
                    "Content-Type": "application/json",
                }
            })
            a.click();
            return datas

        }).then(() => {
            // 这里同样能打印出前面获取到的值，因为是在.then链中依次执行的

        })
            .catch((error) => {
                console.error('出现错误：', error);
            });


    })

    // Your code here...
})();